#ifndef SQL_H
#define SQL_H
#include <mysql/jdbc.h>
#include<ConnectionPool.h>

ConnectionPool connectionPool;
/** 数据插入
 *
 * 入参：info对象跟数据库表字段对应
 */

void insert(const Info& info) {
    std::string placeholder;
    for(int i=0;i<info.props.size();i++){
        placeholder+="?,";
    }
    placeholder.pop_back();
    std::string sql = "insert into " + info.table_name +Info().columns+
            " values ("+placeholder+")";
    sql::Connection* con;
    try {
        con=connectionPool.getConnection();
        if(con!=NULL){
            std::unique_ptr<sql::PreparedStatement> pstmt(con->prepareStatement(sql));
            for(int i=0;i<info.props.size();i++){
                pstmt->setString(i+1,info.props[i]);
            }
            int res = pstmt->executeUpdate();
            if (res < 1) {
                std::cout<<"插入失败！\n"<<std::endl;
                return;
            }
           std::cout<<"插入成功！\n"<<std::endl;
        }
        connectionPool.releaseConnection(con);
    } catch (sql::SQLException &e) {
        connectionPool.releaseConnection(con);
        std::cerr << "SQLException: " << e.what() << std::endl;
    }
}
//批量插入-未完成
void insert_plus(const std::vector<Info>& infoList, sql::Connection* con) {
    std::string placeholder;
    for(int i=0;i<infoList[0].props.size();i++){
        placeholder+="?,";
    }
    placeholder.pop_back();
    std::string sql = "insert into " + infoList[0].table_name +Info().columns+
            " values ("+placeholder+")";
}
#endif // SQL_H
